package com.icngor.action;

import org.apache.log4j.Logger;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import com.icngor.pageModel.DataGrid;
import com.icngor.pageModel.Json;
import com.icngor.pageModel.User;
import com.icngor.service.UserServiceI;
import com.opensymphony.xwork2.ModelDriven;

@Namespace("/")
@Action(value="userAction")
public class UserAction extends BaseAction implements ModelDriven<User>{

	
	private static final Logger logger = Logger.getLogger(UserAction.class);
	
	private User user = new User();
	@Override
	public User getModel() {
		return user;
	}
	
	
	private UserServiceI userService;
	
	public UserServiceI getUserService() {
		return userService;
	}
	
	@Autowired
	public void setUserService(UserServiceI userService) {
		this.userService = userService;
	}

	public void reg(){
		Json j = new Json();
		try{
			userService.save(user);
			j.setSuccess(true);
			j.setMsg("注册成功！");
		}catch(Exception e){
			j.setMsg(e.getMessage());
		}
		
		super.writeJson(j);
	}

	public void login(){
		User u = userService.login(user);
		Json j = new Json();
		if(u!=null){
			j.setSuccess(true);
			j.setMsg("登录成功！");
		}else{
			j.setMsg("登录失败，用户名或密码错误！");
		}
		super.writeJson(j);
	}
	
	public void datagrid(){
		DataGrid dg = userService.datagrid(user);
		super.writeJson(dg);
	}
	
	public void add(){
		Json j = new Json();
		try{
			User u = userService.save(user);
			j.setSuccess(true);
			j.setMsg("添加成功！");
			j.setObj(u);
		}catch(Exception e){
			j.setMsg(e.getMessage());
		}
		
		super.writeJson(j);
	}
	
	public void remove(){
		userService.remove(user.getIds());
		Json j = new Json();
		j.setSuccess(true);
		j.setMsg("删除成功！");
		super.writeJson(j);
	}
	
	public void edit(){
		User u =userService.edit(user);
		Json j = new Json();
		j.setSuccess(true);
		j.setObj(u);
		j.setMsg("修改成功！");
		super.writeJson(j);
	}
}
